/**
 * gome
 * created by 虚拟前端组 on 2016-01-28
 */
var formatAdStr = function(a, b) {
    var c = a.vw, d = a.vh, e = "", f = a.grps || [];
    a.boxid;
    return $.each(f, function(a, g) {
        var h = g.type, i = g.ads || [];
        switch (h) {
            case 2:
                $.each(i, function(a, f) {
                    var g = f.furl || "", h = f.rurl || "";
                    if ("true" == b) {
                        var i = g ? 'target="_blank"' : "";
                        g = g || "javascript:;", e += '<a href="' + g + '" ' + i + '><img alt="充值" src="' + h + '" style="width:' + c + "px;height:" + d + 'px"></a><a class="close-topad" href="#nogo"><i>关闭</i></a>'
                    } else
                        e += g ? '<a href="' + g + '" target="_blank"><img alt="充值" src="' + h + '" style="width:' + c + "px;height:" + d + 'px"></a>' : '<img alt="充值" src="' + h + '" style="width:' + c + "px;height:" + d + 'px">'
                });
                break;
            case 1:
                $.each(f, function(a, b) {
                    var c = b.ads || [], d = "";
                    $.each(c, function(a, b) {
                        var c = b.furl || "";
                        d += c ? '<li><a href="' + c + '" target="_blank">' + b.title + "</a></li>" : "<li>" + b.title + "</li>"
                    }), e += d
                });
                break;
            case 5:
                $.each(f, function(a, b) {
                    var c = b.ads || [], d = "";
                    $.each(c, function(a, b) {
                        var c = b.furl || "";
                        d += c ? '<em><a href="' + c + '" target="_blank">' + b.title + "</a></em>" : "<em>" + b.title + "</em>"
                    }), e += d
                })
            }
    }), e
}, getAd = function() {
    var a = $("[adid]");
    a.each(function() {
        var a = $(this), b = Number(a.attr("adid")), c = a.attr("data-topad"), d = a.attr("okadsh"), e = !1;
        return b && $.ajax({url: "/v1/ad/getADs", type: "get", data: {boxid: b}, success: function(b) {
                var e = b.data || {};
                e && a.html(formatAdStr(e, c)), "true" == d && 0 == e.grps.length && a.attr("noad", "true")
            }, error: function(a, b) {
                e = !0
            }}), e ? !1 : void 0
    })
};
$(function() {
    getAd()
});